package com.sysu.homework.action;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.sysu.homework.model.Group;
import com.sysu.homework.model.Homework;
import com.sysu.homework.model.HomeworkResult;
import com.sysu.homework.model.Student;
import com.sysu.homework.service.GroupService;
import com.sysu.homework.service.HomeworkResultService;
import com.sysu.homework.service.HomeworkService;
import com.sysu.homework.service.StudentService;

@Component("reviewHomeworkAction")
@Scope("prototype")
public class ReviewHomeworkAction extends ActionSupport{
	private HttpSession session = null;
	private String role;
	private int userid;
	
	private StudentService studentService;
	private HomeworkService homeworkService;
	private HomeworkResultService homeworkResultService;
	private GroupService groupService;
	private boolean leader;
	private List<Student> myGroupStudents;
	private List<Homework> allHomeworks;
	private List<Group> allGroups;
	
	private int homeworkid;
	private int groupid;
	private int[] ranks;
	
	private double first;
	private double last;
	
	

	public ReviewHomeworkAction() {
		session = ServletActionContext.getRequest().getSession();
		setRole(session.getAttribute("role").toString());
		setUserid((Integer)session.getAttribute("user"));
		leader = false;
	}

	@Override
	public String execute() throws Exception {
		if(role.equals("student")){
			int groupid = studentService.getGroupId(userid);
			Group group = groupService.getGroupById(groupid);
			if (group!=null && group.getGroupLeader().getId()==userid)
			{
				leader = true;
				myGroupStudents = groupService.getGroupStudentsById(groupid);
				allHomeworks = homeworkService.showHomeworks();
				role = getRole();
				return "success";
			}else
			{
				return "notGroupLeader";
			}
		}else if(role.equals("ta")||role.equals("teacher")){
			allHomeworks = homeworkService.showHomeworks();
			allGroups = groupService.getAllGroups();
			role = getRole();
			return "success";
		}
		return "failed";
	}
	
	public String rank(){
		int groupid = studentService.getGroupId(userid);
		myGroupStudents = groupService.getGroupStudentsById(groupid);
		if (ranks.length>0){
			for (int i=0;i<ranks.length;i++)
			{
				String filename = myGroupStudents.get(i).getId()+"_"+homeworkid+".zip";
				HomeworkResult result = homeworkResultService.getHomeworkResultByFilename(filename);
				result.setRank(ranks[i]);
				homeworkResultService.saveOrUpdate(result);
				System.out.println(myGroupStudents.get(i).getId()+"_"+homeworkid+"_"+ranks[i]+".zip");
			}
			return "rank";
		}
		
		return "failed";
	}
	

	public String grade(){
		
		if(last==0&&first==0&&homeworkid==0&&groupid==0){
			return "failed";
		}
		List<Student> students = groupService.getGroupStudentsById(groupid);
		int size = students.size();
		List<Double> grades = new ArrayList<Double>();
		if (size==1){
			grades.add(0, (last+first)/2);
			
		}else if (size >1){
			for(int i=0;i<size;i++){
				grades.add(i,(first-i*(first-last)/(size-1)));
			}
		}
		if(size>0){
			for (int i=0;i<size;i++) {
				String filename = students.get(i).getId()+"_"+homeworkid+".zip";
				HomeworkResult result = homeworkResultService.getHomeworkResultByFilename(filename);
				result.setGrade(grades.get(i));
				homeworkResultService.saveOrUpdate(result);
			}
			return "grade";
		}
		
		return "failed";
	}
	
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public String getRole() {
		return role;
	}
	public void setRole(String role) {
		this.role = role;
	}

	public void setUserid(int userid) {
		this.userid = userid;
	}

	public int getUserid() {
		return userid;
	}
	

	public StudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(StudentService studentService) {
		this.studentService = studentService;
	}

	public HomeworkService getHomeworkService() {
		return homeworkService;
	}

	@Resource
	public void setHomeworkService(HomeworkService homeworkService) {
		this.homeworkService = homeworkService;
	}

	public HomeworkResultService getHomeworkResultService() {
		return homeworkResultService;
	}

	@Resource
	public void setHomeworkResultService(HomeworkResultService homeworkResultService) {
		this.homeworkResultService = homeworkResultService;
	}

	@Resource
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	public GroupService getGroupService() {
		return groupService;
	}

	public void setLeader(boolean leader) {
		this.leader = leader;
	}

	public boolean isLeader() {
		return leader;
	}
	public List<Student> getMyGroupStudents() {
		return myGroupStudents;
	}

	public void setMyGroupStudents(List<Student> myGroupStudents) {
		this.myGroupStudents = myGroupStudents;
	}

	public List<Homework> getAllHomeworks() {
		return allHomeworks;
	}

	public void setAllHomeworks(List<Homework> allHomeworks) {
		this.allHomeworks = allHomeworks;
	}

	public int getHomeworkid() {
		return homeworkid;
	}

	public void setHomeworkid(int homeworkid) {
		this.homeworkid = homeworkid;
	}

	public int[] getRanks() {
		return ranks;
	}

	public void setRanks(int[] ranks) {
		this.ranks = ranks;
	}

	public void setAllGroups(List<Group> allGroups) {
		this.allGroups = allGroups;
	}

	public List<Group> getAllGroups() {
		return allGroups;
	}

	public int getGroupid() {
		return groupid;
	}

	public void setGroupid(int groupid) {
		this.groupid = groupid;
	}

	public double getFirst() {
		return first;
	}

	public void setFirst(double first) {
		this.first = first;
	}

	public double getLast() {
		return last;
	}

	public void setLast(double last) {
		this.last = last;
	}

}
